/*本模块功能:消息通知
        编写者:朱光曜
        编写时间:2017.11.20*/
$(function() {
	var Alert1 = new Alert;
	var confirm1 = new Confirm();
	var roleId =result.paramData.roleId;
	var emp = false; //判断是否清空需要传后台

	function selectClass(data) {//班级内容
		var str = "";
		for(key in data.classList) {
			str += '<div><input value="0" name="selecteds" type="checkbox" class="oneCheck" id="num' + data.classList[key].classId + '" classId="' + data.classList[key].classId + '"/><label for="num' + data.classList[key].classId + '">' + data.classList[key].className + '</label></div>';
		}
		return str;
	}
	var loading1 = new Loading();//监控全局ajax添加loading
	loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();
		
		});
	$(document).ajaxStop(function(){
		loading1.hide();
	
		});
	var index = {
		init: function() {
			this.bind();
		},
		bind: function() {
			var self = this;
			//roleId=2;
			
			if(roleId == 2) { //跟据身份判断权限(教师)
				self.loadClass();
				$(".selectClass").show(0);
			} else if(roleId == 3 || roleId == 4) { //(管理员)
				self.loadClass();
			}
			$(".clearMessage").click(function() { //清空消息
				var $self = self;
				if(emp) {//判断是否后台有消息
				
					$("#storageDays").val("");
					$(".messageNotificationConText textarea").val("");
					return;
				}

				var parameter = {
					"noticeId": Number($(this).attr("noticeId"))
				};
				
				$.ajax({
					type: "post",
					url: "delNoticeSet",
					async: true,
					data: {
						"jsonStr": JSON.stringify(parameter)
					},

					success: function(msg) {

						var msgNew = JSON.parse(msg);
						var self = $self;
						if(msgNew.result == "success") {
							self.alert1("提交成功","success");

						} else {
							
							//self.alert1("提交失败");
							if(msgNew.errorMsg){
								self.alert1(msgNew.errorMsg);
							}else{
								self.alert1("提交失败");
							}

						}
						//window.location.reload();
					},
					error: function(data, status, xhr) {
						var json = JSON.stringify({
							"pageName": "error"
					    });
						window.location.href = "skipPage?jsonStr=" + json;

					}
				});
			});
			$(".preservation").click(function() { //点击提交
				if(roleId == 2) { //跟据身份判断权限(教师)
					self.delListEvent();
				} else if(roleId == 3 || roleId == 4) { //(管理员)
					var noticeContent = $(".messageNotificationConText textarea").val();
					var n = /^[1-9]\d*$|^0$/;
					var retain = Number($("#storageDays").val());
					var parame = {

						"noticeContent": noticeContent,
						"retain": retain
					};
					
					if(retain == "") {
						self.alert1("请输入保存天数");
					} else if(!n.test(retain)) {
						self.alert1("请输入数字天数");
					} else if(noticeContent == "") {
						self.alert1("请输入消息内容");
					} else if(verification.specialCode(noticeContent) !== true) {
						self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
					} else if(verification.spaceCode(noticeContent) !== true) {
						self.alert1(verification.spaceCode(noticeContent));
					} else {
						confirm1.content = "确定发送这条消息通知吗";
						confirm1.success = function() {
							self.delivery("addNotice", parame);
						};
						confirm1.fail = function() {

						};
						confirm1.init();
						confirm1.show();

					}
				}

			});
		},
		loadClass: function() { //加载班级和内容
			var self = this;
			var par = {};
			$.ajax({
				type: "post",
				url: "AddNoticeView",
				async: true,
				data: {
					"jsonStr": JSON.stringify(par)
				},

				success: function(msg) {
					var msgNew = JSON.parse(msg);
					
					if(roleId == 2) { //跟据身份判断权限(教师)
						$(".messageNotificationCon .selectClass>.selectClassList").html(selectClass(msgNew));
						self.allCheckEvent();
					}
					
					if(msgNew.noticeMap){//判断是否有消息
						var noticeContent=msgNew.noticeMap.noticeContent;
					    $("#storageDays").val(msgNew.noticeMap.retain);
						$(".messageNotificationConText textarea").val(noticeContent.replace(/<br>/g,"\n"));
						$(".clearMessage").attr("noticeId", msgNew.noticeMap.noticeId);	
					
					}else{
						emp = true;
					}
					if(msgNew.noticeClassList){//判断是否有消息
						
						$(".messageNotificationCon .selectClass>div .oneCheck").each(function(){
							//console.log(Number($(this).attr("classId")));
							for(key in msgNew.noticeClassList){
								//console.log(msgNew.noticeClassList[key]);
								if(Number($(this).attr("classId"))==msgNew.noticeClassList[key].classId){
									
									$(this).val(1).attr("checked","checked");
									return true;
								}
							
							}
							
						});
						
						}else{
							emp = true;
						}
					
				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		delivery: function(urls, parameter) { //提交保存信息
			var $self = this;

			
			$.ajax({
				type: "post",
				url: urls,
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameter)
				},

				success: function(msg) {

					var msgNew = JSON.parse(msg);
					var self = $self;
					if(msgNew.result == "success") {
						self.alert1("提交成功","success");

					} else {
						//self.alert1("提交失败");
						if(msgNew.failMsg){
							self.alert1(msgNew.failMsg);
							
						}else if(msgNew.errorMsg){
							self.alert1(msgNew.errorMsg);
						}else{
							self.alert1("提交失败");
						}

					}

				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		alert1: function(content1,state) { //confirm提示

			Alert1.contenthead = "提示";
			Alert1.content = content1;

			Alert1.success = function() {
				if(state=="success"){
					window.location.reload();
				}
				
				Alert1.hide();
			};

			Alert1.init();
			Alert1.show();
		},
		delListEvent: function() { //选择班级
			var classList = [];
			var self = this;
			$(".oneCheck[value=1]").each(function() {
				if(parseInt($(this).val()) == 1) {
					classList.push($(this).attr("classId"));
				}
			});
			var noticeContent = $(".messageNotificationConText textarea").val();
			var n = /^[1-9]\d*$|^0$/;
			var retain = Number($("#storageDays").val());

			var parame = {
				"noticeContent": noticeContent,
				"classList": classList,
				
				"retain": retain
			};
		
			if($(".oneCheck[value=1]").length == 0) {
				Alert1.content = "请选择班级";
				Alert1.init();
				Alert1.show();
			} else if(retain == "") {
				self.alert1("请输入保存天数");
			} else if(!n.test(retain)) {
				self.alert1("请输入数字天数");
			} else if(noticeContent == "") {
				self.alert1("请输入消息内容");
			} else if(verification.specialCode(noticeContent) !== true) {
				self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
			} else {
				var j=0;
				for(i in parame){//遍历改变数字
					
					if(j==0){j++;continue;}
					if(!isNaN(parame[i])&&!$.isArray(parame[i])){
						parame[i]=Number(parame[i]);
					}
				}
				
				
				confirm1.contenthead = "提示";
				confirm1.content = "确定发送这条消息通知吗";
				confirm1.success = function() {
					self.delivery("addNoticeSet", parame);
				};
				confirm1.fail = function() {

				};
				confirm1.init();
				confirm1.show();

			}
			

		},
		allCheckEvent: function() {
			//全选
			allCheck.init("controlAll", "oneCheck");
		}

	};
	index.init();
});